home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / compress / gnucpio.zip / COPYIN.C < prev    next >
C/C++ Source or Header  |  1996-01-01  |  39KB  |  1,351 lines

  1. /* copyin.c - extract or list a cpio archive
  2.    Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include "filetypes.h"
  22. #include "system.h"
  23. #include "cpiohdr.h"
  24. #include "dstring.h"
  25. #include "extern.h"
  26. #include "defer.h"
  27. #include "rmt.h"
  28. #ifndef    FNM_PATHNAME
  29. #include <fnmatch.h>
  30. #endif
  31.  
  32. #ifndef HAVE_LCHOWN
  33. #define lchown chown
  34. #endif
  35.  
  36. static void read_pattern_file ();
  37. static void tape_skip_padding ();
  38. static void defer_copyin ();
  39. static void create_defered_links ();
  40. static void create_final_defers ();
  41.  
  42. /* Return 16-bit integer I with the bytes swapped.  */
  43. #define swab_short(i) ((((i) << 8) & 0xff00) | (((i) >> 8) & 0x00ff))
  44.  
  45. /* Read the header, including the name of the file, from file
  46.    descriptor IN_DES into FILE_HDR.  */
  47.  
  48. void
  49. read_in_header (file_hdr, in_des)
  50.      struct new_cpio_header *file_hdr;
  51.      int in_des;
  52. {
  53.   long bytes_skipped = 0;    /* Bytes of junk found before magic number.  */
  54.  
  55.   /* Search for a valid magic number.  */
  56.  
  57.   if (archive_format == arf_unknown)
  58.     {
  59.       char tmpbuf[512];
  60.       int check_tar;
  61.       int peeked_bytes;
  62.  
  63.       while (archive_format == arf_unknown)
  64.     {
  65.       peeked_bytes = tape_buffered_peek (tmpbuf, in_des, 512);
  66.       if (peeked_bytes < 6)
  67.         error (1, 0, "premature end of archive");
  68.  
  69.       if (!strncmp (tmpbuf, "070701", 6))
  70.         archive_format = arf_newascii;
  71.       else if (!strncmp (tmpbuf, "070707", 6))
  72.         archive_format = arf_oldascii;
  73.       else if (!strncmp (tmpbuf, "070702", 6))
  74.         {
  75.           archive_format = arf_crcascii;
  76.           crc_i_flag = TRUE;
  77.         }
  78.       else if ((*((unsigned short *) tmpbuf) == 070707) ||
  79.            (*((unsigned short *) tmpbuf) == swab_short ((unsigned short) 070707)))
  80.         archive_format = arf_binary;
  81.       else if (peeked_bytes >= 512
  82.            && (check_tar = is_tar_header (tmpbuf)))
  83.         {
  84.           if (check_tar == 2)
  85.         archive_format = arf_ustar;
  86.           else
  87.         archive_format = arf_tar;
  88.         }
  89.       else
  90.         {
  91.           tape_buffered_read ((char *) tmpbuf, in_des, 1L);
  92.           ++bytes_skipped;
  93.         }
  94.     }
  95.     }
  96.  
  97.   if (archive_format == arf_tar || archive_format == arf_ustar)
  98.     {
  99.       if (append_flag)
  100.     last_header_start = input_bytes - io_block_size +
  101.       (in_buff - input_buffer);
  102.       if (bytes_skipped > 0)
  103.     error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
  104.       read_in_tar_header (file_hdr, in_des);
  105.       return;
  106.     }
  107.  
  108.   file_hdr->c_tar_linkname = NULL;
  109.  
  110.   tape_buffered_read ((char *) file_hdr, in_des, 6L);
  111.   while (1)
  112.     {
  113.       if (append_flag)
  114.     last_header_start = input_bytes - io_block_size
  115.       + (in_buff - input_buffer) - 6;
  116.       if (archive_format == arf_newascii
  117.       && !strncmp ((char *) file_hdr, "070701", 6))
  118.     {
  119.       if (bytes_skipped > 0)
  120.         error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
  121.       read_in_new_ascii (file_hdr, in_des);
  122.       break;
  123.     }
  124.       if (archive_format == arf_crcascii
  125.       && !strncmp ((char *) file_hdr, "070702", 6))
  126.     {
  127.       if (bytes_skipped > 0)
  128.         error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
  129.       read_in_new_ascii (file_hdr, in_des);
  130.       break;
  131.     }
  132.       if ( (archive_format == arf_oldascii || archive_format == arf_hpoldascii)
  133.       && !strncmp ((char *) file_hdr, "070707", 6))
  134.     {
  135.       if (bytes_skipped > 0)
  136.         error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
  137.       read_in_old_ascii (file_hdr, in_des);
  138.       break;
  139.     }
  140.       if ( (archive_format == arf_binary || archive_format == arf_hpbinary)
  141.       && (file_hdr->c_magic == 070707
  142.           || file_hdr->c_magic == swab_short ((unsigned short) 070707)))
  143.     {
  144.       /* Having to skip 1 byte because of word alignment is normal.  */
  145.       if (bytes_skipped > 0)
  146.         error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
  147.       read_in_binary (file_hdr, in_des);
  148.       break;
  149.     }
  150.       bytes_skipped++;
  151.       bcopy ((char *) file_hdr + 1, (char *) file_hdr, 5);
  152.       tape_buffered_read ((char *) file_hdr + 5, in_des, 1L);
  153.     }
  154. }
  155.  
  156. /* Fill in FILE_HDR by reading an old-format ASCII format cpio header from
  157.    file descriptor IN_DES, except for the magic number, which is
  158.    already filled in.  */
  159.  
  160. void
  161. read_in_old_ascii (file_hdr, in_des)
  162.      struct new_cpio_header *file_hdr;
  163.      int in_des;
  164. {
  165.   char ascii_header[78];
  166.   unsigned long dev;
  167.   unsigned long rdev;
  168.  
  169.   tape_buffered_read (ascii_header, in_des, 70L);
  170.   ascii_header[70] = '\0';
  171.   sscanf (ascii_header,
  172.       "%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6lo%11lo",
  173.       &dev, &file_hdr->c_ino,
  174.       &file_hdr->c_mode, &file_hdr->c_uid, &file_hdr->c_gid,
  175.       &file_hdr->c_nlink, &rdev, &file_hdr->c_mtime,
  176.       &file_hdr->c_namesize, &file_hdr->c_filesize);
  177.   file_hdr->c_dev_maj = major (dev);
  178.   file_hdr->c_dev_min = minor (dev);
  179.   file_hdr->c_rdev_maj = major (rdev);
  180.   file_hdr->c_rdev_min = minor (rdev);
  181.  
  182.   /* Read file name from input.  */
  183.   if (file_hdr->c_name != NULL)
  184.     free (file_hdr->c_name);
  185.   file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize + 1);
  186.   tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
  187. #ifndef __MSDOS__
  188.   /* HP/UX cpio creates archives that look just like ordinary archives,
  189.      but for devices it sets major = 0, minor = 1, and puts the
  190.      actual major/minor number in the filesize field.  See if this
  191.      is an HP/UX cpio archive, and if so fix it.  We have to do this
  192.      here because process_copy_in() assumes filesize is always 0
  193.      for devices.  */
  194.   switch (file_hdr->c_mode & CP_IFMT)
  195.     {
  196.       case CP_IFCHR:
  197.       case CP_IFBLK:
  198. #ifdef CP_IFSOCK
  199.       case CP_IFSOCK:
  200. #endif
  201. #ifdef CP_IFIFO
  202.       case CP_IFIFO:
  203. #endif
  204.     if (file_hdr->c_filesize != 0
  205.         && file_hdr->c_rdev_maj == 0
  206.         && file_hdr->c_rdev_min == 1)
  207.       {
  208.         file_hdr->c_rdev_maj = major (file_hdr->c_filesize);
  209.         file_hdr->c_rdev_min = minor (file_hdr->c_filesize);
  210.         file_hdr->c_filesize = 0;
  211.       }
  212.     break;
  213.       default:
  214.     break;
  215.     }
  216. #endif  /* __MSDOS__ */
  217. }
  218.  
  219. /* Fill in FILE_HDR by reading a new-format ASCII format cpio header from
  220.    file descriptor IN_DES, except for the magic number, which is
  221.    already filled in.  */
  222.  
  223. void
  224. read_in_new_ascii (file_hdr, in_des)
  225.      struct new_cpio_header *file_hdr;
  226.      int in_des;
  227. {
  228.   char ascii_header[112];
  229.  
  230.   tape_buffered_read (ascii_header, in_des, 104L);
  231.   ascii_header[104] = '\0';
  232.   sscanf (ascii_header,
  233.       "%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx",
  234.       &file_hdr->c_ino, &file_hdr->c_mode, &file_hdr->c_uid,
  235.       &file_hdr->c_gid, &file_hdr->c_nlink, &file_hdr->c_mtime,
  236.       &file_hdr->c_filesize, &file_hdr->c_dev_maj, &file_hdr->c_dev_min,
  237.     &file_hdr->c_rdev_maj, &file_hdr->c_rdev_min, &file_hdr->c_namesize,
  238.       &file_hdr->c_chksum);
  239.   /* Read file name from input.  */
  240.   if (file_hdr->c_name != NULL)
  241.     free (file_hdr->c_name);
  242.   file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize);
  243.   tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
  244.  
  245.   /* In SVR4 ASCII format, the amount of space allocated for the header
  246.      is rounded up to the next long-word, so we might need to drop
  247.      1-3 bytes.  */
  248.   tape_skip_padding (in_des, file_hdr->c_namesize + 110);
  249. }
  250.  
  251. /* Fill in FILE_HDR by reading a binary format cpio header from
  252.    file descriptor IN_DES, except for the first 6 bytes (the magic
  253.    number, device, and inode number), which are already filled in.  */
  254.  
  255. void
  256. read_in_binary (file_hdr, in_des)
  257.      struct new_cpio_header *file_hdr;
  258.      int in_des;
  259. {
  260.   struct old_cpio_header short_hdr;
  261.  
  262.   /* Copy the data into the short header, then later transfer
  263.      it into the argument long header.  */
  264.   short_hdr.c_dev = ((struct old_cpio_header *) file_hdr)->c_dev;
  265.   short_hdr.c_ino = ((struct old_cpio_header *) file_hdr)->c_ino;
  266.   tape_buffered_read (((char *) &short_hdr) + 6, in_des, 20L);
  267.  
  268.   /* If the magic number is byte swapped, fix the header.  */
  269.   if (file_hdr->c_magic == swab_short ((unsigned short) 070707))
  270.     {
  271.       static int warned = 0;
  272.  
  273.       /* Alert the user that they might have to do byte swapping on
  274.      the file contents.  */
  275.       if (warned == 0)
  276.     {
  277.       error (0, 0, "warning: archive header has reverse byte-order");
  278.       warned = 1;
  279.     }
  280.       swab_array ((char *) &short_hdr, 13);
  281.     }
  282.  
  283.   file_hdr->c_dev_maj = major (short_hdr.c_dev);
  284.   file_hdr->c_dev_min = minor (short_hdr.c_dev);
  285.   file_hdr->c_ino = short_hdr.c_ino;
  286.   file_hdr->c_mode = short_hdr.c_mode;
  287.   file_hdr->c_uid = short_hdr.c_uid;
  288.   file_hdr->c_gid = short_hdr.c_gid;
  289.   file_hdr->c_nlink = short_hdr.c_nlink;
  290.   file_hdr->c_rdev_maj = major (short_hdr.c_rdev);
  291.   file_hdr->c_rdev_min = minor (short_hdr.c_rdev);
  292.   file_hdr->c_mtime = (unsigned long) short_hdr.c_mtimes[0] << 16
  293.     | short_hdr.c_mtimes[1];
  294.  
  295.   file_hdr->c_namesize = short_hdr.c_namesize;
  296.   file_hdr->c_filesize = (unsigned long) short_hdr.c_filesizes[0] << 16
  297.     | short_hdr.c_filesizes[1];
  298.  
  299.   /* Read file name from input.  */
  300.   if (file_hdr->c_name != NULL)
  301.     free (file_hdr->c_name);
  302.   file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize);
  303.   tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
  304.  
  305.   /* In binary mode, the amount of space allocated in the header for
  306.      the filename is `c_namesize' rounded up to the next short-word,
  307.      so we might need to drop a byte.  */
  308.   if (file_hdr->c_namesize % 2)
  309.     tape_toss_input (in_des, 1L);
  310.  
  311. #ifndef __MSDOS__
  312.   /* HP/UX cpio creates archives that look just like ordinary archives,
  313.      but for devices it sets major = 0, minor = 1, and puts the
  314.      actual major/minor number in the filesize field.  See if this
  315.      is an HP/UX cpio archive, and if so fix it.  We have to do this
  316.      here because process_copy_in() assumes filesize is always 0
  317.      for devices.  */
  318.   switch (file_hdr->c_mode & CP_IFMT)
  319.     {
  320.       case CP_IFCHR:
  321.       case CP_IFBLK:
  322. #ifdef CP_IFSOCK
  323.       case CP_IFSOCK:
  324. #endif
  325. #ifdef CP_IFIFO
  326.       case CP_IFIFO:
  327. #endif
  328.     if (file_hdr->c_filesize != 0
  329.         && file_hdr->c_rdev_maj == 0
  330.         && file_hdr->c_rdev_min == 1)
  331.       {
  332.         file_hdr->c_rdev_maj = major (file_hdr->c_filesize);
  333.         file_hdr->c_rdev_min = minor (file_hdr->c_filesize);
  334.         file_hdr->c_filesize = 0;
  335.       }
  336.     break;
  337.       default:
  338.     break;
  339.     }
  340. #endif  /* __MSDOS__ */
  341. }
  342.  
  343. /* Exchange the bytes of each element of the array of COUNT shorts
  344.    starting at PTR.  */
  345.  
  346. void
  347. swab_array (ptr, count)
  348.      char *ptr;
  349.      int count;
  350. {
  351.   char tmp;
  352.  
  353.   while (count-- > 0)
  354.     {
  355.       tmp = *ptr;
  356.       *ptr = *(ptr + 1);
  357.       ++ptr;
  358.       *ptr = tmp;
  359.       ++ptr;
  360.     }
  361. }
  362.  
  363. /* Current time for verbose table.  */
  364. static time_t current_time;
  365.  
  366. /* Read the collection from standard input and create files
  367.    in the file system.  */
  368.  
  369. void
  370. process_copy_in ()
  371. {
  372.   char done = FALSE;        /* True if trailer reached.  */
  373.   int res;            /* Result of various function calls.  */
  374.   dynamic_string new_name;    /* New file name for rename option.  */
  375.   FILE *tty_in;            /* Interactive file for rename option.  */
  376.   FILE *tty_out;        /* Interactive file for rename option.  */
  377.   FILE *rename_in;        /* Batch file for rename option.  */
  378.   char *str_res;        /* Result for string function.  */
  379.   struct utimbuf times;        /* For setting file times.  */
  380.   struct stat file_stat;    /* Output file stat record.  */
  381.   struct new_cpio_header file_hdr;    /* Output header information.  */
  382.   int out_file_des;        /* Output file descriptor.  */
  383.   int in_file_des;        /* Input file descriptor.  */
  384.   char skip_file;        /* Flag for use with patterns.  */
  385.   int existing_dir;        /* True if file is a dir & already exists.  */
  386.   int i;            /* Loop index variable.  */
  387.   char *link_name = NULL;    /* Name of hard and symbolic links.  */
  388. #ifdef HPUX_CDF
  389.   int cdf_flag;                 /* True if file is a CDF.  */
  390.   int cdf_char;                 /* Index of `+' char indicating a CDF.  */
  391. #endif
  392.  
  393.   /* Initialize the copy in.  */
  394.   if (pattern_file_name)
  395.     read_pattern_file ();
  396.   file_hdr.c_name = NULL;
  397.   ds_init (&new_name, 128);
  398.   /* Initialize this in case it has members we don't know to set.  */
  399.   bzero (×, sizeof (struct utimbuf));
  400.  
  401.   if (rename_batch_file)
  402.     {
  403.       rename_in = fopen (rename_batch_file, "r");
  404.       if (rename_in == NULL)
  405.     error (2, errno, CONSOLE);
  406.     }
  407.   else if (rename_flag)
  408.     {
  409.       /* Open interactive file pair for rename operation.  */
  410.       tty_in = fopen (CONSOLE, "r");
  411.       if (tty_in == NULL)
  412.     error (2, errno, CONSOLE);
  413.       tty_out = fopen (CONSOLE, "w");
  414.       if (tty_out == NULL)
  415.     error (2, errno, CONSOLE);
  416.     }
  417.  
  418.   /* Get date and time if needed for processing the table option.  */
  419.   if (table_flag && verbose_flag)
  420.     time (¤t_time);
  421.  
  422. #ifdef __MSDOS__
  423.   setmode (archive_des, O_BINARY);
  424. #endif
  425.   /* Check whether the input file might be a tape.  */
  426.   in_file_des = archive_des;
  427.   if (_isrmt (in_file_des))
  428.     {
  429.       input_is_special = 1;
  430.       input_is_seekable = 0;
  431.     }
  432.   else
  433.     {
  434.       if (fstat (in_file_des, &file_stat))
  435.     error (1, errno, "standard input is closed");
  436.       input_is_special =
  437. #ifdef S_ISBLK
  438.     S_ISBLK (file_stat.st_mode) ||
  439. #endif
  440.     S_ISCHR (file_stat.st_mode);
  441.       input_is_seekable = S_ISREG (file_stat.st_mode);
  442.     }
  443.   output_is_seekable = TRUE;
  444.  
  445.   /* While there is more input in the collection, process the input.  */
  446.   while (!done)
  447.     {
  448.       link_name = NULL;
  449.       swapping_halfwords = swapping_bytes = FALSE;
  450.  
  451.       /* Start processing the next file by reading the header.  */
  452.       read_in_header (&file_hdr, in_file_des);
  453.  
  454. #ifdef DEBUG_CPIO
  455.       if (debug_flag)
  456.     {
  457.       struct new_cpio_header *h;
  458.       h = &file_hdr;
  459.       fprintf (stderr, 
  460.         "magic = 0%o, ino = %d, mode = 0%o, uid = %d, gid = %d\n",
  461.         h->c_magic, h->c_ino, h->c_mode, h->c_uid, h->c_gid);
  462.       fprintf (stderr, 
  463.         "nlink = %d, mtime = %d, filesize = %d, dev_maj = 0x%x\n",
  464.         h->c_nlink, h->c_mtime, h->c_filesize, h->c_dev_maj);
  465.       fprintf (stderr, 
  466.             "dev_min = 0x%x, rdev_maj = 0x%x, rdev_min = 0x%x, namesize = %d\n",
  467.         h->c_dev_min, h->c_rdev_maj, h->c_rdev_min, h->c_namesize);
  468.       fprintf (stderr, 
  469.         "chksum = %d, name = \"%s\", tar_linkname = \"%s\"\n",
  470.         h->c_chksum, h->c_name, 
  471.         h->c_tar_linkname ? h->c_tar_linkname : "(null)" );
  472.  
  473.     }
  474. #endif
  475.       /* Is this the header for the TRAILER file?  */
  476.       if (strcmp ("TRAILER!!!", file_hdr.c_name) == 0)
  477.     {
  478.       done = TRUE;
  479.       break;
  480.     }
  481.  
  482.       /* Do we have to ignore absolute paths, and if so, does the filename
  483.          have an absolute path?  */
  484.       if (no_abs_paths_flag && file_hdr.c_name && file_hdr.c_name [0] == '/')
  485.     {
  486.       char *p;
  487.  
  488.       p = file_hdr.c_name;
  489.       while (*p == '/')
  490.         ++p;
  491.       if (*p == '\0')
  492.         {
  493.           strcpy (file_hdr.c_name, ".");
  494.         }
  495.       else
  496.         {
  497.           char *non_abs_name;
  498.  
  499.           non_abs_name = (char *) xmalloc (strlen (p) + 1);
  500.           strcpy (non_abs_name, p);
  501.           free (file_hdr.c_name);
  502.           file_hdr.c_name = non_abs_name;
  503.         }
  504.     }
  505.  
  506.       /* Does the file name match one of the given patterns?  */
  507.       if (num_patterns <= 0)
  508.     skip_file = FALSE;
  509.       else
  510.     {
  511.       skip_file = copy_matching_files;
  512.       for (i = 0; i < num_patterns
  513.            && skip_file == copy_matching_files; i++)
  514.         {
  515.           if (fnmatch (save_patterns[i], file_hdr.c_name, 0) == 0)
  516.         skip_file = !copy_matching_files;
  517.         }
  518.     }
  519.  
  520.       if (skip_file)
  521.     {
  522.       tape_toss_input (in_file_des, file_hdr.c_filesize);
  523.       tape_skip_padding (in_file_des, file_hdr.c_filesize);
  524.     }
  525.       else if (table_flag)
  526.     {
  527.       if (verbose_flag)
  528.         {
  529. #ifdef CP_IFLNK
  530.           if ((file_hdr.c_mode & CP_IFMT) == CP_IFLNK)
  531.         {
  532.           if (archive_format != arf_tar && archive_format != arf_ustar)
  533.             {
  534.               link_name = (char *) xmalloc ((unsigned int) file_hdr.c_filesize + 1);
  535.               link_name[file_hdr.c_filesize] = '\0';
  536.               tape_buffered_read (link_name, in_file_des, file_hdr.c_filesize);
  537.               long_format (&file_hdr, link_name);
  538.               free (link_name);
  539.               tape_skip_padding (in_file_des, file_hdr.c_filesize);
  540.               continue;
  541.             }
  542.           else
  543.             {
  544.               long_format (&file_hdr, file_hdr.c_tar_linkname);
  545.               continue;
  546.             }
  547.         }
  548.           else
  549. #endif
  550.         long_format (&file_hdr, (char *) 0);
  551.         }
  552.       else
  553.         printf ("%s\n", file_hdr.c_name);
  554.  
  555.       crc = 0;
  556.       tape_toss_input (in_file_des, file_hdr.c_filesize);
  557.       tape_skip_padding (in_file_des, file_hdr.c_filesize);
  558.       if (only_verify_crc_flag)
  559.         {
  560. #ifdef CP_IFLNK
  561.           if ((file_hdr.c_mode & CP_IFMT) == CP_IFLNK)
  562.         continue;   /* links don't have a checksum */
  563. #endif
  564.           if (crc != file_hdr.c_chksum)
  565.         error (0, 0, "%s: checksum error (0x%x, should be 0x%x)",
  566.                file_hdr.c_name, crc, file_hdr.c_chksum);
  567.         }
  568.     }
  569.       else if (append_flag)
  570.     {
  571.       tape_toss_input (in_file_des, file_hdr.c_filesize);
  572.       tape_skip_padding (in_file_des, file_hdr.c_filesize);
  573.     }
  574.       else if (only_verify_crc_flag)
  575.     {
  576. #ifdef CP_IFLNK
  577.       if ((file_hdr.c_mode & CP_IFMT) == CP_IFLNK)
  578.         {
  579.           if (archive_format != arf_tar && archive_format != arf_ustar)
  580.         {
  581.           tape_toss_input (in_file_des, file_hdr.c_filesize);
  582.           tape_skip_padding (in_file_des, file_hdr.c_filesize);
  583.           continue;
  584.         }
  585.         }
  586. #endif
  587.         crc = 0;
  588.         tape_toss_input (in_file_des, file_hdr.c_filesize);
  589.         tape_skip_padding (in_file_des, file_hdr.c_filesize);
  590.         if (crc != file_hdr.c_chksum)
  591.           error (0, 0, "%s: checksum error (0x%x, should be 0x%x)",
  592.              file_hdr.c_name, crc, file_hdr.c_chksum);
  593.     }
  594.       else
  595.     {
  596.       /* Copy the input file into the directory structure.  */
  597.  
  598.       /* Do we need to rename the file? */
  599.       if (rename_flag || rename_batch_file)
  600.         {
  601.           if (rename_flag)
  602.         {
  603.           fprintf (tty_out, "rename %s -> ", file_hdr.c_name);
  604.           fflush (tty_out);
  605.           str_res = ds_fgets (tty_in, &new_name);
  606.         }
  607.           else
  608.         {
  609.           str_res = ds_fgetstr (rename_in, &new_name, '\n');
  610.         }
  611.           if (str_res == NULL || str_res[0] == 0)
  612.         {
  613.           tape_toss_input (in_file_des, file_hdr.c_filesize);
  614.           tape_skip_padding (in_file_des, file_hdr.c_filesize);
  615.           continue;
  616.         }
  617.           else
  618.         file_hdr.c_name = xstrdup (new_name.ds_string);
  619.         }
  620.  
  621.       /* See if the file already exists.  */
  622.       existing_dir = FALSE;
  623.       if (lstat (file_hdr.c_name, &file_stat) == 0)
  624.         {
  625.           if (S_ISDIR (file_stat.st_mode)
  626.           && ((file_hdr.c_mode & CP_IFMT) == CP_IFDIR))
  627.         {
  628.           /* If there is already a directory there that
  629.              we are trying to create, don't complain about
  630.              it.  */
  631.           existing_dir = TRUE;
  632.         }
  633.           else if (!unconditional_flag
  634.                && file_hdr.c_mtime <= file_stat.st_mtime)
  635.         {
  636.           error (0, 0, "%s not created: newer or same age version exists",
  637.              file_hdr.c_name);
  638.           tape_toss_input (in_file_des, file_hdr.c_filesize);
  639.           tape_skip_padding (in_file_des, file_hdr.c_filesize);
  640.           continue;    /* Go to the next file.  */
  641.         }
  642.           else if (S_ISDIR (file_stat.st_mode) 
  643.             ? rmdir (file_hdr.c_name)
  644.             : unlink (file_hdr.c_name))
  645.         {
  646.           error (0, errno, "cannot remove current %s",
  647.              file_hdr.c_name);
  648.           tape_toss_input (in_file_des, file_hdr.c_filesize);
  649.           tape_skip_padding (in_file_des, file_hdr.c_filesize);
  650.           continue;    /* Go to the next file.  */
  651.         }
  652.         }
  653.  
  654.       /* Do the real copy or link.  */
  655.       switch (file_hdr.c_mode & CP_IFMT)
  656.         {
  657.         case CP_IFREG:
  658. #ifndef __MSDOS__
  659.           /* Can the current file be linked to a previously copied file? */
  660.           if (file_hdr.c_nlink > 1 && (archive_format == arf_newascii
  661.           || archive_format == arf_crcascii) )
  662.         {
  663.           int link_res;
  664.           if (file_hdr.c_filesize == 0)
  665.             {
  666.               /* The newc and crc formats store multiply linked copies
  667.              of the same file in the archive only once.  The
  668.              actual data is attached to the last link in the
  669.              archive, and the other links all have a filesize
  670.              of 0.  Since this file has multiple links and a
  671.              filesize of 0, its data is probably attatched to
  672.              another file in the archive.  Save the link, and
  673.              process it later when we get the actual data.  We
  674.              can't just create it with length 0 and add the
  675.              data later, in case the file is readonly.  We still
  676.              lose if its parent directory is readonly (and we aren't
  677.              running as root), but there's nothing we can do about
  678.              that.  */
  679.               defer_copyin (&file_hdr);
  680.               tape_toss_input (in_file_des, file_hdr.c_filesize);
  681.               tape_skip_padding (in_file_des, file_hdr.c_filesize);
  682.               break;
  683.             }
  684.           /* If the file has data (filesize != 0), then presumably
  685.              any other links have already been defer_copyin'ed(),
  686.              but GNU cpio version 2.0-2.2 didn't do that, so we
  687.              still have to check for links here (and also in case
  688.              the archive was created and later appeneded to). */
  689.           link_res = link_to_maj_min_ino (file_hdr.c_name, 
  690.                 file_hdr.c_dev_maj, file_hdr.c_dev_maj,
  691.                 file_hdr.c_ino);
  692.           if (link_res == 0)
  693.             {
  694.               tape_toss_input (in_file_des, file_hdr.c_filesize);
  695.               tape_skip_padding (in_file_des, file_hdr.c_filesize);
  696.               break;
  697.             }
  698.         }
  699.           else if (file_hdr.c_nlink > 1 && archive_format != arf_tar
  700.           && archive_format != arf_ustar)
  701.         {
  702.           int link_res;
  703.           link_res = link_to_maj_min_ino (file_hdr.c_name, 
  704.                 file_hdr.c_dev_maj, file_hdr.c_dev_maj,
  705.                 file_hdr.c_ino);
  706.           if (link_res == 0)
  707.             {
  708.               tape_toss_input (in_file_des, file_hdr.c_filesize);
  709.               tape_skip_padding (in_file_des, file_hdr.c_filesize);
  710.               break;
  711.             }
  712.         }
  713.           else if ((archive_format == arf_tar || archive_format == arf_ustar)
  714.                && file_hdr.c_tar_linkname && 
  715.                file_hdr.c_tar_linkname[0] != '\0')
  716.         {
  717.           int    link_res;
  718.           link_res = link_to_name (file_hdr.c_name,
  719.                          file_hdr.c_tar_linkname);
  720.           if (link_res < 0)
  721.             {
  722.               error (0, errno, "cannot link %s to %s",
  723.                  file_hdr.c_tar_linkname, file_hdr.c_name);
  724.             }
  725.           break;
  726.         }
  727. #endif
  728.  
  729.           /* If not linked, copy the contents of the file.  */
  730.           if (link_name == NULL)
  731.         {
  732.           out_file_des = open (file_hdr.c_name,
  733.                        O_CREAT | O_WRONLY | O_BINARY, 0600);
  734.           if (out_file_des < 0 && create_dir_flag)
  735.             {
  736.               create_all_directories (file_hdr.c_name);
  737.               out_file_des = open (file_hdr.c_name,
  738.                        O_CREAT | O_WRONLY | O_BINARY,
  739.                        0600);
  740.             }
  741.           if (out_file_des < 0)
  742.             {
  743.               error (0, errno, "%s", file_hdr.c_name);
  744.               tape_toss_input (in_file_des, file_hdr.c_filesize);
  745.               tape_skip_padding (in_file_des, file_hdr.c_filesize);
  746.               continue;
  747.             }
  748.  
  749.           crc = 0;
  750.           if (swap_halfwords_flag)
  751.             {
  752.               if ((file_hdr.c_filesize % 4) == 0)
  753.             swapping_halfwords = TRUE;
  754.               else
  755.             error (0, 0, "cannot swap halfwords of %s: odd number of halfwords",
  756.                    file_hdr.c_name);
  757.             }
  758.           if (swap_bytes_flag)
  759.             {
  760.               if ((file_hdr.c_filesize % 2) == 0)
  761.             swapping_bytes = TRUE;
  762.               else
  763.             error (0, 0, "cannot swap bytes of %s: odd number of bytes",
  764.                    file_hdr.c_name);
  765.             }
  766.           copy_files_tape_to_disk (in_file_des, out_file_des, file_hdr.c_filesize);
  767.           disk_empty_output_buffer (out_file_des);
  768.           if (close (out_file_des) < 0)
  769.             error (0, errno, "%s", file_hdr.c_name);
  770.  
  771.           if (archive_format == arf_crcascii)
  772.             {
  773.               if (crc != file_hdr.c_chksum)
  774.             error (0, 0, "%s: checksum error (0x%x, should be 0x%x)",
  775.                    file_hdr.c_name, crc, file_hdr.c_chksum);
  776.             }
  777.           /* File is now copied; set attributes.  */
  778.           if (!no_chown_flag)
  779.             if ((chown (file_hdr.c_name,
  780.                 set_owner_flag ? set_owner : file_hdr.c_uid,
  781.                set_group_flag ? set_group : file_hdr.c_gid) < 0)
  782.             && errno != EPERM)
  783.               error (0, errno, "%s", file_hdr.c_name);
  784.           /* chown may have turned off some permissions we wanted. */
  785.           if (chmod (file_hdr.c_name, (int) file_hdr.c_mode) < 0)
  786.             error (0, errno, "%s", file_hdr.c_name);
  787.           if (retain_time_flag)
  788.             {
  789.               times.actime = times.modtime = file_hdr.c_mtime;
  790.               if (utime (file_hdr.c_name, ×) < 0)
  791.             error (0, errno, "%s", file_hdr.c_name);
  792.             }
  793.           tape_skip_padding (in_file_des, file_hdr.c_filesize);
  794.           if (file_hdr.c_nlink > 1 && (archive_format == arf_newascii
  795.               || archive_format == arf_crcascii) )
  796.             {
  797.               /* (see comment above for how the newc and crc formats 
  798.              store multiple links).  Now that we have the data 
  799.              for this file, create any other links to it which
  800.              we defered.  */
  801.               create_defered_links (&file_hdr);
  802.             }
  803.         }
  804.           break;
  805.  
  806.         case CP_IFDIR:
  807.           /* Strip any trailing `/'s off the filename; tar puts
  808.          them on.  We might as well do it here in case anybody
  809.          else does too, since they cause strange things to happen.  */
  810.           strip_trailing_slashes (file_hdr.c_name);
  811.  
  812.           /* Ignore the current directory.  It must already exist,
  813.          and we don't want to change its permission, ownership
  814.          or time.  */
  815.           if (file_hdr.c_name[0] == '.' && file_hdr.c_name[1] == '\0')
  816.         break;
  817.  
  818. #ifdef HPUX_CDF
  819.           cdf_flag = 0;
  820. #endif
  821.           if (!existing_dir)
  822.  
  823.         {
  824. #ifdef HPUX_CDF
  825.           /* If the directory name ends in a + and is SUID,
  826.              then it is a CDF.  Strip the trailing + from
  827.              the name before creating it.  */
  828.           cdf_char = strlen (file_hdr.c_name) - 1;
  829.           if ( (cdf_char > 0) &&
  830.                (file_hdr.c_mode & 04000) && 
  831.                (file_hdr.c_name [cdf_char] == '+') )
  832.             {
  833.               file_hdr.c_name [cdf_char] = '\0';
  834.               cdf_flag = 1;
  835.             }
  836. #endif
  837.           res = mkdir (file_hdr.c_name, file_hdr.c_mode);
  838.         }
  839.           else
  840.         res = 0;
  841.           if (res < 0 && create_dir_flag)
  842.         {
  843.           create_all_directories (file_hdr.c_name);
  844.           res = mkdir (file_hdr.c_name, file_hdr.c_mode);
  845.         }
  846.           if (res < 0)
  847.         {
  848.           /* In some odd cases where the file_hdr.c_name includes `.',
  849.              the directory may have actually been created by
  850.              create_all_directories(), so the mkdir will fail
  851.              because the directory exists.  If that's the case,
  852.              don't complain about it.  */
  853.           if ( (errno != EEXIST) ||
  854.                (lstat (file_hdr.c_name, &file_stat) != 0) ||
  855.                !(S_ISDIR (file_stat.st_mode) ) )
  856.             {
  857.               error (0, errno, "%s", file_hdr.c_name);
  858.               continue;
  859.             }
  860.         }
  861.           if (!no_chown_flag)
  862.         if ((chown (file_hdr.c_name,
  863.                 set_owner_flag ? set_owner : file_hdr.c_uid,
  864.                 set_group_flag ? set_group : file_hdr.c_gid) < 0)
  865.             && errno != EPERM)
  866.           error (0, errno, "%s", file_hdr.c_name);
  867.           /* chown may have turned off some permissions we wanted. */
  868.           if (chmod (file_hdr.c_name, (int) file_hdr.c_mode) < 0)
  869.         error (0, errno, "%s", file_hdr.c_name);
  870. #ifdef HPUX_CDF
  871.           if (cdf_flag)
  872.         /* Once we "hide" the directory with the chmod(),
  873.            we have to refer to it using name+ instead of name.  */
  874.         file_hdr.c_name [cdf_char] = '+';
  875. #endif
  876.           if (retain_time_flag)
  877.         {
  878.           times.actime = times.modtime = file_hdr.c_mtime;
  879.           if (utime (file_hdr.c_name, ×) < 0)
  880.             error (0, errno, "%s", file_hdr.c_name);
  881.         }
  882.           break;
  883.  
  884. #ifndef __MSDOS__
  885.         case CP_IFCHR:
  886.         case CP_IFBLK:
  887. #ifdef CP_IFSOCK
  888.         case CP_IFSOCK:
  889. #endif
  890. #ifdef CP_IFIFO
  891.         case CP_IFIFO:
  892. #endif
  893.           if (file_hdr.c_nlink > 1 && archive_format != arf_tar
  894.           && archive_format != arf_ustar)
  895.         {
  896.           int link_res;
  897.           link_res = link_to_maj_min_ino (file_hdr.c_name, 
  898.                 file_hdr.c_dev_maj, file_hdr.c_dev_maj,
  899.                 file_hdr.c_ino);
  900.           if (link_res == 0)
  901.             break;
  902.         }
  903.           else if (archive_format == arf_ustar &&
  904.                file_hdr.c_tar_linkname && 
  905.                file_hdr.c_tar_linkname [0] != '\0')
  906.         {
  907.           int    link_res;
  908.           link_res = link_to_name (file_hdr.c_name,
  909.                        file_hdr.c_tar_linkname);
  910.           if (link_res < 0)
  911.             {
  912.               error (0, errno, "cannot link %s to %s",
  913.                  file_hdr.c_tar_linkname, file_hdr.c_name);
  914.               /* Something must be wrong, because we couldn't
  915.              find the file to link to.  But can we assume
  916.              that the device maj/min numbers are correct
  917.              and fall through to the mknod?  It's probably
  918.              safer to just break, rather than possibly
  919.              creating a bogus device file.  */
  920.             }
  921.           break;
  922.         }
  923.           
  924.           res = mknod (file_hdr.c_name, file_hdr.c_mode,
  925.             makedev (file_hdr.c_rdev_maj, file_hdr.c_rdev_min));
  926.           if (res < 0 && create_dir_flag)
  927.         {
  928.           create_all_directories (file_hdr.c_name);
  929.           res = mknod (file_hdr.c_name, file_hdr.c_mode,
  930.             makedev (file_hdr.c_rdev_maj, file_hdr.c_rdev_min));
  931.         }
  932.           if (res < 0)
  933.         {
  934.           error (0, errno, "%s", file_hdr.c_name);
  935.           continue;
  936.         }
  937.           if (!no_chown_flag)
  938.         if ((chown (file_hdr.c_name,
  939.                 set_owner_flag ? set_owner : file_hdr.c_uid,
  940.                 set_group_flag ? set_group : file_hdr.c_gid) < 0)
  941.             && errno != EPERM)
  942.           error (0, errno, "%s", file_hdr.c_name);
  943.           /* chown may have turned off some permissions we wanted. */
  944.           if (chmod (file_hdr.c_name, file_hdr.c_mode) < 0)
  945.         error (0, errno, "%s", file_hdr.c_name);
  946.           if (retain_time_flag)
  947.         {
  948.           times.actime = times.modtime = file_hdr.c_mtime;
  949.           if (utime (file_hdr.c_name, ×) < 0)
  950.             error (0, errno, "%s", file_hdr.c_name);
  951.         }
  952.           break;
  953. #endif
  954.  
  955. #ifdef CP_IFLNK
  956.         case CP_IFLNK:
  957.           {
  958.         if (archive_format != arf_tar && archive_format != arf_ustar)
  959.           {
  960.             link_name = (char *) xmalloc ((unsigned int) file_hdr.c_filesize + 1);
  961.             link_name[file_hdr.c_filesize] = '\0';
  962.             tape_buffered_read (link_name, in_file_des, file_hdr.c_filesize);
  963.             tape_skip_padding (in_file_des, file_hdr.c_filesize);
  964.           }
  965.         else
  966.           {
  967.             link_name = xstrdup (file_hdr.c_tar_linkname);
  968.           }
  969.  
  970.         res = UMASKED_SYMLINK (link_name, file_hdr.c_name,
  971.                        file_hdr.c_mode);
  972.         if (res < 0 && create_dir_flag)
  973.           {
  974.             create_all_directories (file_hdr.c_name);
  975.             res = UMASKED_SYMLINK (link_name, file_hdr.c_name,
  976.                        file_hdr.c_mode);
  977.           }
  978.         if (res < 0)
  979.           {
  980.             error (0, errno, "%s", file_hdr.c_name);
  981.             free (link_name);
  982.             link_name = NULL;
  983.             continue;
  984.           }
  985.         if (!no_chown_flag)
  986.           if ((lchown (file_hdr.c_name,
  987.                    set_owner_flag ? set_owner : file_hdr.c_uid,
  988.                set_group_flag ? set_group : file_hdr.c_gid) < 0)
  989.               && errno != EPERM)
  990.             error (0, errno, "%s", file_hdr.c_name);
  991.         free (link_name);
  992.         link_name = NULL;
  993.           }
  994.           break;
  995. #endif
  996.  
  997.         default:
  998.           error (0, 0, "%s: unknown file type", file_hdr.c_name);
  999.           tape_toss_input (in_file_des, file_hdr.c_filesize);
  1000.           tape_skip_padding (in_file_des, file_hdr.c_filesize);
  1001.         }
  1002.  
  1003.       if (verbose_flag)
  1004.         fprintf (stderr, "%s\n", file_hdr.c_name);
  1005.       if (dot_flag)
  1006.         fputc ('.', stderr);
  1007.     }
  1008.     }
  1009.  
  1010.   if (dot_flag)
  1011.     fputc ('\n', stderr);
  1012.  
  1013.   if (append_flag)
  1014.     return;
  1015.  
  1016.   if (archive_format == arf_newascii || archive_format == arf_crcascii)
  1017.     create_final_defers ();
  1018.   if (!quiet_flag)
  1019.     {
  1020.       res = (input_bytes + io_block_size - 1) / io_block_size;
  1021.       if (res == 1)
  1022.     fprintf (stderr, "1 block\n");
  1023.       else
  1024.     fprintf (stderr, "%d blocks\n", res);
  1025.     }
  1026. }
  1027.  
  1028. /* Print the file described by FILE_HDR in long format.
  1029.    If LINK_NAME is nonzero, it is the name of the file that
  1030.    this file is a symbolic link to.  */
  1031.  
  1032. void
  1033. long_format (file_hdr, link_name)
  1034.      struct new_cpio_header *file_hdr;
  1035.      char *link_name;
  1036. {
  1037.   char mbuf[11];
  1038.   char tbuf[40];
  1039.   time_t when;
  1040.  
  1041.   mode_string (file_hdr->c_mode, mbuf);
  1042.   mbuf[10] = '\0';
  1043.  
  1044.   /* Get time values ready to print.  */
  1045.   when = file_hdr->c_mtime;
  1046.   strcpy (tbuf, ctime (&when));
  1047.   if (current_time - when > 6L * 30L * 24L * 60L * 60L
  1048.       || current_time - when < 0L)
  1049.     {
  1050.       /* The file is older than 6 months, or in the future.
  1051.      Show the year instead of the time of day.  */
  1052.       strcpy (tbuf + 11, tbuf + 19);
  1053.     }
  1054.   tbuf[16] = '\0';
  1055.  
  1056.   printf ("%s %3u ", mbuf, file_hdr->c_nlink);
  1057.  
  1058.   if (numeric_uid)
  1059.     printf ("%-8u %-8u ", (unsigned int) file_hdr->c_uid,
  1060.         (unsigned int) file_hdr->c_gid);
  1061. #ifndef __MSDOS__
  1062.   else
  1063.     printf ("%-8.8s %-8.8s ", getuser (file_hdr->c_uid),
  1064.         getgroup (file_hdr->c_gid));
  1065.  
  1066.   if ((file_hdr->c_mode & CP_IFMT) == CP_IFCHR
  1067.       || (file_hdr->c_mode & CP_IFMT) == CP_IFBLK)
  1068.     printf ("%3u, %3u ", file_hdr->c_rdev_maj,
  1069.         file_hdr->c_rdev_min);
  1070.   else
  1071. #endif
  1072.     printf ("%8lu ", file_hdr->c_filesize);
  1073.  
  1074.   printf ("%s ", tbuf + 4);
  1075.  
  1076.   print_name_with_quoting (file_hdr->c_name);
  1077.   if (link_name)
  1078.     {
  1079.       printf (" -> ");
  1080.       print_name_with_quoting (link_name);
  1081.     }
  1082.   putc ('\n', stdout);
  1083. }
  1084.  
  1085. void
  1086. print_name_with_quoting (p)
  1087.      register char *p;
  1088. {
  1089.   register unsigned char c;
  1090.  
  1091.   while ( (c = *p++) )
  1092.     {
  1093.       switch (c)
  1094.     {
  1095. #ifndef __MSDOS__
  1096.     case '\\':
  1097.       printf ("\\\\");
  1098.       break;
  1099. #endif
  1100.  
  1101.     case '\n':
  1102.       printf ("\\n");
  1103.       break;
  1104.  
  1105.     case '\b':
  1106.       printf ("\\b");
  1107.       break;
  1108.  
  1109.     case '\r':
  1110.       printf ("\\r");
  1111.       break;
  1112.  
  1113.     case '\t':
  1114.       printf ("\\t");
  1115.       break;
  1116.  
  1117.     case '\f':
  1118.       printf ("\\f");
  1119.       break;
  1120.  
  1121.     case ' ':
  1122.       printf ("\\ ");
  1123.       break;
  1124.  
  1125.     case '"':
  1126.       printf ("\\\"");
  1127.       break;
  1128.  
  1129.     default:
  1130.       if (c > 040 &&
  1131. #ifdef __MSDOS__
  1132.           c < 0377 && c != 0177
  1133. #else
  1134.           c < 0177
  1135. #endif
  1136.         )
  1137.         putchar (c);
  1138.       else
  1139.         printf ("\\%03o", (unsigned int) c);
  1140.     }
  1141.     }
  1142. }
  1143.  
  1144. /* Read a pattern file (for the -E option).  Put a list of
  1145.    `num_patterns' elements in `save_patterns'.  Any patterns that were
  1146.    already in `save_patterns' (from the command line) are preserved.  */
  1147.  
  1148. static void
  1149. read_pattern_file ()
  1150. {
  1151.   int max_new_patterns;
  1152.   char **new_save_patterns;
  1153.   int new_num_patterns;
  1154.   int i;
  1155.   dynamic_string pattern_name;
  1156.   FILE *pattern_fp;
  1157.  
  1158.   if (num_patterns < 0)
  1159.     num_patterns = 0;
  1160.   max_new_patterns = 1 + num_patterns;
  1161.   new_save_patterns = (char **) xmalloc (max_new_patterns * sizeof (char *));
  1162.   new_num_patterns = num_patterns;
  1163.   ds_init (&pattern_name, 128);
  1164.  
  1165.   pattern_fp = fopen (pattern_file_name, "r");
  1166.   if (pattern_fp == NULL)
  1167.     error (1, errno, "%s", pattern_file_name);
  1168.   while (ds_fgetstr (pattern_fp, &pattern_name, '\n') != NULL)
  1169.     {
  1170.       if (new_num_patterns >= max_new_patterns)
  1171.     {
  1172.       max_new_patterns += 1;
  1173.       new_save_patterns = (char **)
  1174.         xrealloc ((char *) new_save_patterns,
  1175.               max_new_patterns * sizeof (char *));
  1176.     }
  1177.       new_save_patterns[new_num_patterns] = xstrdup (pattern_name.ds_string);
  1178.       ++new_num_patterns;
  1179.     }
  1180.   if (ferror (pattern_fp) || fclose (pattern_fp) == EOF)
  1181.     error (1, errno, "%s", pattern_file_name);
  1182.  
  1183.   for (i = 0; i < num_patterns; ++i)
  1184.     new_save_patterns[i] = save_patterns[i];
  1185.  
  1186.   save_patterns = new_save_patterns;
  1187.   num_patterns = new_num_patterns;
  1188. }
  1189.  
  1190. /* Skip the padding on IN_FILE_DES after a header or file,
  1191.    up to the next header.
  1192.    The number of bytes skipped is based on OFFSET -- the current offset
  1193.    from the last start of a header (or file) -- and the current
  1194.    header type.  */
  1195.  
  1196. static void
  1197. tape_skip_padding (in_file_des, offset)
  1198.      int in_file_des;
  1199.      int offset;
  1200. {
  1201.   int pad;
  1202.  
  1203.   if (archive_format == arf_crcascii || archive_format == arf_newascii)
  1204.     pad = (4 - (offset % 4)) % 4;
  1205.   else if (archive_format == arf_binary || archive_format == arf_hpbinary)
  1206.     pad = (2 - (offset % 2)) % 2;
  1207.   else if (archive_format == arf_tar || archive_format == arf_ustar)
  1208.     pad = (512 - (offset % 512)) % 512;
  1209.   else
  1210.     pad = 0;
  1211.  
  1212.   if (pad != 0)
  1213.     tape_toss_input (in_file_des, pad);
  1214. }
  1215.  
  1216.  
  1217. /* The newc and crc formats store multiply linked copies of the same file 
  1218.    in the archive only once.  The actual data is attached to the last link 
  1219.    in the archive, and the other links all have a filesize of 0.  When a 
  1220.    file in the archive has multiple links and a filesize of 0, its data is 
  1221.    probably "attatched" to another file in the archive, so we can't create
  1222.    it right away.  We have to "defer" creating it until we have created
  1223.    the file that has the data "attatched" to it.  We keep a list of the
  1224.    "defered" links on deferments.  */
  1225.  
  1226. struct deferment *deferments = NULL;
  1227.  
  1228. /* Add a file header to the deferments list.  For now they all just
  1229.    go on one list, although we could optimize this if necessary.  */
  1230.  
  1231. static void
  1232. defer_copyin (file_hdr)
  1233.   struct new_cpio_header *file_hdr;
  1234. {
  1235.   struct deferment *d;
  1236.   d = create_deferment (file_hdr);
  1237.   d->next = deferments;
  1238.   deferments = d;
  1239.   return;
  1240. }
  1241.  
  1242. /* We just created a file that (probably) has some other links to it
  1243.    which have been defered.  Go through all of the links on the deferments
  1244.    list and create any which are links to this file.  */
  1245.  
  1246. static void
  1247. create_defered_links (file_hdr)
  1248.   struct new_cpio_header *file_hdr;
  1249. {
  1250.   struct deferment *d;
  1251.   struct deferment *d_prev;
  1252.   int    ino;
  1253.   int     maj;
  1254.   int   min;
  1255.   int     link_res;
  1256.   ino = file_hdr->c_ino;
  1257.   maj = file_hdr->c_dev_maj;
  1258.   min = file_hdr->c_dev_min;
  1259.   d = deferments;
  1260.   d_prev = NULL;
  1261.   while (d != NULL)
  1262.     {
  1263.       if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
  1264.       && (d->header.c_dev_min == min) )
  1265.     {
  1266.       struct deferment *d_free;
  1267.       link_res = link_to_name (d->header.c_name, file_hdr->c_name);
  1268.       if (link_res < 0)
  1269.         {
  1270.           error (0, errno, "cannot link %s to %s",
  1271.              d->header.c_name, file_hdr->c_name);
  1272.         }
  1273.       if (d_prev != NULL)
  1274.         d_prev->next = d->next;
  1275.       else
  1276.         deferments = d->next;
  1277.       d_free = d;
  1278.       d = d->next;
  1279.       free_deferment (d_free);
  1280.     }
  1281.       else
  1282.     {
  1283.       d_prev = d;
  1284.       d = d->next;
  1285.     }
  1286.     }
  1287. }
  1288.  
  1289. /* If we had a multiply linked file that really was empty then we would
  1290.    have defered all of its links, since we never found any with data
  1291.    "attached", and they will still be on the deferment list even when
  1292.    we are done reading the whole archive.  Write out all of these
  1293.    empty links that are still on the deferments list.  */
  1294.  
  1295. static void
  1296. create_final_defers ()
  1297. {
  1298.   struct deferment *d;
  1299.   int    link_res;
  1300.   int    out_file_des;
  1301.   struct utimbuf times;        /* For setting file times.  */
  1302.   /* Initialize this in case it has members we don't know to set.  */
  1303.   bzero (×, sizeof (struct utimbuf));
  1304.   
  1305.   for (d = deferments; d != NULL; d = d->next)
  1306.     {
  1307.       d = deferments;
  1308.       link_res = link_to_maj_min_ino (d->header.c_name, 
  1309.             d->header.c_dev_maj, d->header.c_dev_maj,
  1310.             d->header.c_ino);
  1311.       if (link_res == 0)
  1312.     {
  1313.       continue;
  1314.     }
  1315.       out_file_des = open (d->header.c_name,
  1316.                O_CREAT | O_WRONLY | O_BINARY, 0600);
  1317.       if (out_file_des < 0 && create_dir_flag)
  1318.     {
  1319.       create_all_directories (d->header.c_name);
  1320.       out_file_des = open (d->header.c_name,
  1321.                    O_CREAT | O_WRONLY | O_BINARY,
  1322.                    0600);
  1323.     }
  1324.       if (out_file_des < 0)
  1325.     {
  1326.       error (0, errno, "%s", d->header.c_name);
  1327.       continue;
  1328.     }
  1329.  
  1330.       if (close (out_file_des) < 0)
  1331.     error (0, errno, "%s", d->header.c_name);
  1332.  
  1333.       /* File is now copied; set attributes.  */
  1334.       if (!no_chown_flag)
  1335.     if ((chown (d->header.c_name,
  1336.             set_owner_flag ? set_owner : d->header.c_uid,
  1337.            set_group_flag ? set_group : d->header.c_gid) < 0)
  1338.         && errno != EPERM)
  1339.       error (0, errno, "%s", d->header.c_name);
  1340.       /* chown may have turned off some permissions we wanted. */
  1341.       if (chmod (d->header.c_name, (int) d->header.c_mode) < 0)
  1342.     error (0, errno, "%s", d->header.c_name);
  1343.       if (retain_time_flag)
  1344.     {
  1345.       times.actime = times.modtime = d->header.c_mtime;
  1346.       if (utime (d->header.c_name, ×) < 0)
  1347.         error (0, errno, "%s", d->header.c_name);
  1348.     }
  1349.     }
  1350. }
  1351.